#!/bin/bash
# config_update_packaging.sh - Configuration Update and Packaging script
# This script replicates the functionality of the :cuw function from launcher.cmd
# It handles directory rebuilding, file copying, and packaging operations

# Define variables similar to launcher.cmd
WORKSPACE="${HOME}/workspace"
DEMO_ROOT="${WORKSPACE}/demo"
DEPLOY="${WORKSPACE}/preDeploy"

echo "Configuration Update and Packaging Script"
echo "========================================="
echo "Workspace: ${WORKSPACE}"
echo "Demo Root: ${DEMO_ROOT}"
echo "Deploy Dir: ${DEPLOY}"
echo ""

# Recreate deploy directory
echo "Rebuilding preDeploy directory..."
rm -rf "${DEPLOY}"
mkdir -p "${DEPLOY}"
mkdir -p "${DEPLOY}/temp"
mkdir -p "${DEPLOY}/html"
mkdir -p "${DEPLOY}/www_static/s_meta/"
mkdir -p "${DEPLOY}/www_static/b_html/"
mkdir -p "${DEPLOY}/www_static/b_html/cert/"

# Copy Matrix Java jar file
MATRIX_JAR_PATH="${WORKSPACE}/demo/matrix_java/target/matrix_java-1.0.0.RELEASE.jar"
if [ -f "${MATRIX_JAR_PATH}" ]; then
    echo "Copying Matrix Java jar file to ${DEPLOY}/www_static/s_meta/"
    cp "${MATRIX_JAR_PATH}" "${DEPLOY}/www_static/s_meta/"
    echo "Matrix Java jar file copied successfully."
else
    echo "Warning: Matrix Java jar file not found at ${MATRIX_JAR_PATH}"
fi

# Copy static files to deployment directory
echo "Copying static files..."
if [ -d "${WORKSPACE}/demo/matrix_html" ]; then
    echo "Copying files from ${WORKSPACE}/demo/matrix_html to ${DEPLOY}/html"
    cp -r "${WORKSPACE}/demo/matrix_html"/* "${DEPLOY}/html/"
    echo "Static files copied successfully."
else
    echo "Warning: Source directory does not exist: ${WORKSPACE}/demo/matrix_html"
fi

# Copy shell scripts
echo "Copying shell scripts..."
cp -f ${DEMO_ROOT}/matrix_shell/scripts/plugins/*.sh ${DEPLOY}/www_static/ 2>/dev/null || true
cp -f ${DEMO_ROOT}/matrix_shell/scripts/plugins/*.sh ${DEPLOY}/html/ 2>/dev/null || true

# Package HTML to WAR
echo "Packaging HTML to WAR..."
cd ${DEPLOY}/html

if command -v jar >/dev/null 2>&1; then
    jar -cvf ../html.war *
    echo "html.war created using jar command"
elif command -v python >/dev/null 2>&1; then
    python -c "import zipfile, os; f=zipfile.ZipFile('../html.war','w',zipfile.ZIP_DEFLATED); [f.write(os.path.join(r,n),os.path.join(r,n)) for r,d,_ in os.walk('.') for n in _]; f.close()"
    echo "html.war created using Python zip method"
elif command -v zip >/dev/null 2>&1; then
    zip -r ../html.war *
    echo "html.war created using zip command"
else
    echo "Warning: No suitable tool found to create WAR file"
fi

# Packaging related operations
echo "Performing packaging operations..."
cd ${DEPLOY}/www_static

# Copy scripts to b_html directory
for script in *.sh; do
    if [ -f "$script" ]; then
        cp "$script" "${DEPLOY}/www_static/b_html/" 2>/dev/null || true
    fi
done

cp go_java_mac.sh "${DEPLOY}/www_static/s_meta/go.sh" 2>/dev/null || true

# Remove scripts from www_static
rm -f *.sh

# Create final package
cd ${DEPLOY}
if [ -f "html.war" ]; then
    # Copy html.war to b_html directory
    cp html.war www_static/b_html/
    echo "html.war copied to b_html directory"
    
    # Create final package
    cd ${DEPLOY}/www_static
    if command -v tar >/dev/null 2>&1; then
        tar -cvf tmp.tar b_html
        echo "tmp.tar created successfully with html.war included"
    else
        echo "Warning: tar command not available to create final package"
    fi
else
    echo "Error: html.war not found. Package creation failed."
    cd ${DEPLOY}/www_static
fi

# Clean up
rm -rf b_html

echo ""
echo "Configuration update and packaging operations completed."